package com.myworkspace.standard.common.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;


@Component
@WebFilter(urlPatterns = "{/**}", filterName = "testFilter")
public class TestFilter implements Filter {
    private static final Logger LOG = LoggerFactory.getLogger(TestFilter.class);


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        System.out.println(filterConfig.getFilterName() + "初始化");
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long l = System.currentTimeMillis();
        // 执行前
        chain.doFilter(request, response);
        // 执行后
        LOG.info("执行时间：{}", System.currentTimeMillis() - l);
    }

    @Override
    public void destroy() {
        System.out.println("销毁");
        Filter.super.destroy();
    }
}
